﻿using DryIoc;
using DryIoc.ImTools;
using L4_Dialog.Views;
using Prism.Commands;
using Prism.Dialogs;
using Prism.Mvvm;

namespace L4_Dialog.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        IDialogService _dialogService;

        private string _title = "Prism Application";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        #region 弹窗
        public DelegateCommand ChangeTitle { get; set; }

        void ExecuteChangeTitle()
        {
            DialogParameters dialogParameters = new DialogParameters();
            dialogParameters.Add("title", Title);

            _dialogService.ShowDialog("MessageDialog", dialogParameters, result =>
            {
                if (result.Result == ButtonResult.OK)
                {
                    Title = result.Parameters.GetValue<string>("title");
                }
            });
        }
        #endregion

        #region 弹窗-Callback
        public DelegateCommand ChangeTitle2 { get; set; }

        void ExecuteChangeTitle2()
        {
            DialogParameters dialogParameters = new DialogParameters();
            dialogParameters.Add("title", Title);

            DialogCallback dck = new DialogCallback();
            dck.OnClose(result =>
            {
                if (result.Result == ButtonResult.OK)
                {
                    Title = result.Parameters.GetValue<string>("title");
                }
            });

            dck.OnError(ex =>
            {
                Title = ex.Message;
            });

            _dialogService.ShowDialog("MessageDialog", dialogParameters, dck);
        }
        #endregion

        #region 弹窗-单个修改窗口样式
        public DelegateCommand ChangeTitle3 { get; set; }

        void ExecuteChangeTitle3()
        {
            DialogParameters parameters = new DialogParameters();
            parameters.Add("title", Title);

            _dialogService.ShowDialog("EditDialog", parameters, result =>
            {
                if (result.Result == ButtonResult.OK)
                {
                    Title = result.Parameters.GetValue<string>("title");
                }
            });
        }
        #endregion

        #region 弹窗-指定窗口-Show
        public DelegateCommand ChangeTitle4 { get; set; }

        void ExecuteChangeTitle4()
        {
            DialogParameters parameters = new DialogParameters();
            parameters.Add("title", Title);

            _dialogService.Show("MessageDialog", parameters, result =>
            {
                if (result.Result == ButtonResult.OK)
                {
                    Title = result.Parameters.GetValue<string>("title");
                }
            }, "dialogWindowA");
        }
        #endregion

        #region 弹窗-指定窗口-ShowDialog
        public DelegateCommand ChangeTitle5 { get; set; }

        void ExecuteChangeTitle5()
        {
            DialogParameters parameters = new DialogParameters();
            parameters.Add("title", Title);
            parameters.Add("windowName", "dialogWindowA");

            _dialogService.ShowDialog("MessageDialog", parameters, result =>
            {
                if (result.Result == ButtonResult.OK)
                {
                    Title = result.Parameters.GetValue<string>("title");
                }
            });
        }
        #endregion

        public MainWindowViewModel(IDialogService dialogService)
        {
            _dialogService = dialogService;
            ChangeTitle = new DelegateCommand(ExecuteChangeTitle);
            ChangeTitle2 = new DelegateCommand(ExecuteChangeTitle2);
            ChangeTitle3 = new DelegateCommand(ExecuteChangeTitle3);
            ChangeTitle4 = new DelegateCommand(ExecuteChangeTitle4);
            ChangeTitle5 = new DelegateCommand(ExecuteChangeTitle5);
        }
    }
}